package com.antis.olsl.data.distribution;

import android.text.TextUtils;

import com.antis.olsl.base.net.BaseSubscriber;
import com.antis.olsl.base.net.ExceptionHandle;
import com.antis.olsl.base.utils.LogUtils;
import com.antis.olsl.base.utils.RxUtil;
import com.antis.olsl.data.net.ApiServer;
import com.antis.olsl.data.net.RetrofitManager;
import com.antis.olsl.data.response.GetDistributionSlipListRes;
import com.antis.olsl.library.MyApplication;
import com.antis.olsl.library.utils.RSAEncryptUtil;

import java.util.Map;

public class DistributionQueryPresenter implements DistributionQueryContract.Presenter {
    private DistributionQueryContract.View mView;
    private ApiServer mApiServer;

    DistributionQueryPresenter() {
        mApiServer = RetrofitManager.getInstance().getServerApi();
    }

    @Override
    public void takeView(DistributionQueryContract.View view) {
        mView = view;
    }

    @Override
    public void getDistributionList(Map<String, Object> map) {
        String encryptString = RSAEncryptUtil.generateEncryptStr(map);
        String token = null != MyApplication.getUserInfo() && !TextUtils.isEmpty(MyApplication.getUserInfo().getToken()) ? MyApplication.getUserInfo().getToken() : "123456";

        LogUtils.e("hhh", "getDistributionList map= " + map);
        mApiServer.getDistributionList(encryptString, token, 2).compose(RxUtil.<GetDistributionSlipListRes>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<GetDistributionSlipListRes>bindToLife())
                .subscribe(new BaseSubscriber<GetDistributionSlipListRes>() {
                    @Override
                    public void onNext(GetDistributionSlipListRes resp) {
                        mView.getDistributionListSuccess(resp);
                    }

                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.getDistributionListFailure();
                    }
                });
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unSubscribe() {
        LogUtils.e("DistributionQueryPresenter", "unSubscribe");
        mView = null;
    }
}
